package com.xl98.service.impl;

import com.xl98.service.sendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
public class sendMailImpl implements sendMailService {
    @Autowired
    JavaMailSender mailSender;
    @Autowired
    Environment environment;
    //发送简单邮件
    @Override
    public void sendMail(String subject, String text, String to){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject(subject);
        mailMessage.setText(text);
        mailMessage.setTo(to);
        mailMessage.setFrom(environment.getProperty("spring.mail.username"));
        mailSender.send(mailMessage);
    }
    //发送带附件邮件
    @Override
    public void sendMail(String subject, String text, String filemane, String to) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject(subject);
        helper.setText(text,true);
        String path = "src/main/resources/static/img/"+filemane;
        System.out.println(path);
        helper.addAttachment(filemane,new File(path));
        helper.setTo(to);
        helper.setFrom(environment.getProperty("spring.mail.username"));
        mailSender.send(mimeMessage);
    }
}
